﻿using HtmlAgilityPack;
using Newtonsoft.Json;
using Serilog;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using XjjXmm.FrameWork.Cache;
using XjjXmm.Spider.biquge;

namespace XjjXmm.Spider
{
    internal class NovelSpiderManager
    {
        //https://www.zhihu.com/question/359676590/answer/1615531107

        private List<NovelSpider> novelSpiders = new List<NovelSpider>();
        public NovelSpiderManager(ISqlSugarClient sqlSugarClient, IEnumerable<ISiteSpider> siteSpiders)
        {
            foreach (var siteSpider in siteSpiders.Where(t=>t.GetSetting()?.IsActived == true))
            {
                novelSpiders.Add(new NovelSpider(sqlSugarClient, siteSpider));
            }
           
        }



        public void Run()
        {
            List<Task> tasks = new List<Task>();
            novelSpiders.ForEach(
                x => tasks.Add(x.Run())
            );

            Task.WaitAll(tasks.ToArray());
        }

    }
}
